module AboutYou
  module SDK
    module Model
      ###
      # This class represents a Sale Counts model.
      #
      # author:: Collins GmbH & Co KG
      ###
      class SaleCounts
        # count of products in sale
        attr_accessor :product_count_in_sale
        # count of prodocuts not in sale
        attr_accessor :product_count_not_in_sale
        # count of total products
        attr_accessor :product_count_total
        # count of products without facet
        attr_accessor :product_count_without_any_facet
        # count of products with other facets
        attr_accessor :product_count_with_other_facet

        ###
        # the Constructor for the sale counts class
        #
        # * *Args*    :
        #   - +product_count_total+ -> the total productcount
        #   - +product_count_with_other_facet+ -> the productcount without any facets
        #   - +product_count_without_any_facet+ -> the productcount with other facets
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::SaleCounts
        ###
        def initialize(product_count_total,
            product_count_with_other_facet,
            product_count_without_any_facet
          )
          self.product_count_total             = product_count_total
          self.product_count_with_other_facet  = product_count_with_other_facet
          self.product_count_without_any_facet = product_count_without_any_facet
        end

        ###
        # This method is used for creating an instance of this class by a json_object.
        #
        # * *Args*    :
        #   - +json_object+ -> the jsonObject received from the api
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::SaleCounts
        ###
        def self.create_from_json(json_object)
          sale_counts = new(
            json_object['total'],
            json_object['other'],
            json_object['missing']
          )
          sale_counts.parse_terms(json_object['terms'])

          sale_counts
        end

        ###
        # This method is used for parsing the terms of the json_object
        #
        # * *Args*    :
        #   - +jsonTerms+ -> the terms of the json_object received from the api
        ###
        def parse_terms(json_terms)
          json_terms.each do |term|
            if term['term'] == 0
              self.product_count_not_in_sale = term['count']
            else
              self.product_count_in_sale     = term['count']
            end
          end
        end
      end
    end
  end
end
